package cn.zl.model;

import cn.yunbamboos.Model;
import cn.yunbamboos.mybatis.annotation.FieldStrategy;
import cn.yunbamboos.mybatis.annotation.FieldType;
import cn.yunbamboos.mybatis.annotation.TableField;
import cn.yunbamboos.mybatis.annotation.TableName;
import cn.yunbamboos.mybatis.annotation.TablePrimaryKey;
import cn.yunbamboos.rest.service.anno.param.ParamType;
import cn.yunbamboos.rest.service.anno.param.enums.Type;
import com.alibaba.fastjson2.JSONObject;


@TableName(value = "zl_user", cn = "用户表")
public class User implements Model {

    @ParamType(path = "user_id", type = Type.Long, title = "用户ID", required = true, example = "1")
    @TablePrimaryKey(value = "user_id", type = FieldType.Long, length = 64)
    private String userId;

    @ParamType(path = "login_name", type = Type.String, title = "登陆账号", required = true, example = "admin")
    @TableField(value = "login_name", type = FieldType.String, length = 64)
    private String loginName;

    @ParamType(path = "nick_name", type = Type.String, title = "用户昵称", required = true, example = "管理员")
    @TableField(value = "nick_name", type = FieldType.String, length = 64)
    private String nickName;

    @ParamType(path = "password", type = Type.String, title = "密码", required = true, example = "123456")
    @TableField(value = "password", updateStrategy = FieldStrategy.NEVER, type = FieldType.String, length = 64)
    private String password;

    @ParamType(path = "create_time", type = Type.String, title = "创建时间", required = true, example = "2024-01-01 00:00:00")
    @TableField(value = "create_time", type = FieldType.DateTime)
    private String createTime;

    @Override
    public JSONObject encode() {
        JSONObject json = new JSONObject();
        json.put("user_id", String.valueOf(userId));
        json.put("login_name", loginName);
        json.put("nick_name", nickName);
        json.put("create_time", createTime);
        return json;
    }

    @Override
    public void decode(JSONObject json) {
        if (json.containsKey("user_id")) this.userId = json.getString("user_id");
        if (json.containsKey("login_name")) this.loginName = json.getString("login_name");
        if (json.containsKey("nick_name")) this.nickName = json.getString("nick_name");
        if (json.containsKey("password")) this.password = json.getString("password");
        if (json.containsKey("create_time")) {
            this.createTime = json.getString("create_time");
        }
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getLoginName() {
        return loginName;
    }

    public void setLoginName(String loginName) {
        this.loginName = loginName;
    }

    public String getNickName() {
        return nickName;
    }

    public void setNickName(String nickName) {
        this.nickName = nickName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getCreateTime() {
        return createTime;
    }

    public void setCreateTime(String createTime) {
        this.createTime = createTime;
    }

}
